package com.gdzn.netty.channel;

import io.netty.channel.ChannelHandlerContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 管道缓存map
 * @author: cxz kenny
 * @created: 2021-10-27 18:02:09
 * @version: V1.0
 */
public class ChannelTcpMap {
    public static int channelTcpNum = 0;
    private static Map<String, ChannelHandlerContext> channelHashMap = null;

    public static Map<String, ChannelHandlerContext> getChannelHashMap() {
        return channelHashMap;
    }


    public static ChannelHandlerContext getChannelByName(String name) {
        if (null == channelHashMap || channelHashMap.isEmpty()) {
            return null;
        }
        return channelHashMap.get(name);
    }

    public static void addChannel(String name, ChannelHandlerContext cxt){
        if (null == channelHashMap || channelHashMap.isEmpty()) {
            channelHashMap = new ConcurrentHashMap<String, ChannelHandlerContext>();
        }
        channelHashMap.put(name, cxt);
        channelTcpNum++;
    }

    public static int removeChannelByName(String name) {
        if (null == channelHashMap || channelHashMap.isEmpty()) {
            return 0;
        }
        if(channelHashMap.containsKey(name)){
            channelHashMap.remove(name);
            channelTcpNum--;
            return 1;
        }
        return 0;
    }
}

